package com.maychat.controller;

import com.maychat.controller.base.BaseController;
import com.maychat.enums.ContactType;
import com.maychat.enums.JoinType;
import com.maychat.enums.UserContactStatus;
import com.maychat.exception.BusinessException;
import com.maychat.exception.NoAuthException;
import com.maychat.pojo.*;
import com.maychat.pojo.extended.UserContactApplyWithGroupNameAndNickName;
import com.maychat.service.UserContactService;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/contact")
public class UserContactController extends BaseController {
    @Autowired
    private UserContactService userContactService;

    @GetMapping("/search")
    public Result search(@RequestParam @NotBlank String targetId) throws NoAuthException, BusinessException {
        UserInfoToken tokenObj = getTokenObj();
        Map<String, Object> mp = userContactService.searchById(tokenObj.getUserId(), targetId);
        return Result.success(mp);
    }

    @PostMapping("/applyAdd")
    public Result applyAdd(@NotBlank String targetId, String applyInfo) throws NoAuthException, BusinessException {
        UserInfoToken tokenObj = getTokenObj();
        JoinType joinType = userContactService.addApply(tokenObj, targetId, applyInfo);
        Map<String, Integer> mp = new HashMap<>();
        mp.put("joinType", joinType.ordinal());
        return Result.success(mp);
    }

    @GetMapping("/loadApply")
    public Result loadApply(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize
    ) throws NoAuthException {
        UserInfoToken tokenObj = getTokenObj();
        RowsWithTotal<UserContactApplyWithGroupNameAndNickName> res = userContactService.selectUserContactPageByUserId(tokenObj.getUserId(), page,
                pageSize);
        return Result.success(res);
    }

    @PutMapping("/dealApply")
    public Result dealApply(@NotNull Integer applyId, @NotNull Integer actionType) throws NoAuthException, BusinessException {
        UserInfoToken tokenObj = getTokenObj();
        userContactService.dealApply(tokenObj.getUserId(), applyId, actionType);
        return Result.success();
    }

    @GetMapping("/loadContact")
    public Result loadContact(@RequestParam @NotNull Integer contactType) throws BusinessException, NoAuthException {
        ContactType type = ContactType.getByOrdinal(contactType);
        if (type == null) throw new BusinessException();
        UserInfoToken tokenObj = getTokenObj();
        switch (type) {
            case USER -> {
                List<UserInfo> userInfoList = userContactService.selectFriendUsers(tokenObj.getUserId());
                return Result.success(userInfoList);
            }
            case GROUP -> {
                List<GroupInfo> groupInfos = userContactService.selectFriendGroups(tokenObj.getUserId());
                return Result.success(groupInfos);
            }
        }
        throw new BusinessException();
    }

    @DeleteMapping("/delContact")
    public Result delContact(@NotNull String contactId) throws NoAuthException {
        UserInfoToken tokenObj = getTokenObj();
        userContactService.rmUserContact(tokenObj.getUserId(), contactId, UserContactStatus.DELETE);
        return Result.success();
    }

    @DeleteMapping("/banContact")
    public Result banContact(@NotNull String contactId) throws NoAuthException {
        UserInfoToken tokenObj = getTokenObj();
        userContactService.rmUserContact(tokenObj.getUserId(), contactId, UserContactStatus.BAN);
        return Result.success();
    }
}
